package com.soso.base.util.other;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
	
	public static String replaceAll(String regex,String content,ReplaceRule replaceRule,int group){
		StringBuffer sb = new StringBuffer(); 
		Pattern pattern = Pattern.compile(regex,Pattern.DOTALL);
		Matcher matcher = pattern.matcher(content);
		while(matcher.find()) {
			String data=matcher.group(group);
			if(data!=null
					 && !"".equals(data.trim())){
				String temp=replaceRule.modify(matcher.group(), data);
				if(temp!=null){
					matcher.appendReplacement(sb,temp);
				}
			}
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	public static String replaceAll(String regex,String content,String replacement){
		return Pattern.compile(regex,Pattern.DOTALL).matcher(content).replaceAll(replacement);
	}
	public interface ReplaceRule{
		String modify(String dataAll,String data);
	}

}
